﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Planer.Model;
using Planer.UI.Forms;
using System.Data.Objects.DataClasses;
using Planer.Logic;

namespace Planer.UI.Controls
{
    public partial class BillControl : UserControl
    {
        private billing bill = null;

        public billing Bill
        {
            get { return bill; }
            set 
            {
                if (value == null) return;
                bill = value;

                groupBoxSummary.Text = bill.name;
                labelBalance.Text = bill.balance.ToString("C");
                labelCreateDate.Text = String.Format("{0:d}", bill.create_date);
                labelModifyDate.Text = String.Format("{0:d}", bill.modify_date);

                allocation allocation = AllocationModel.GetInstance().GetAllocationForAccumulationByBilling(bill);
                numericUpDownPersents.Value = (int)allocation.amount;

                List<periodical_action> periodicalActions = PeriodicalActionModel.GetInstance().GetPeriodicalActionsByBilling(bill);
                List<allocation> allocations = AllocationModel.GetInstance().GetAllocationsForGoalsByBilling(bill);
                List<action> actions = ActionModel.GetInstance().GetActionsByBilling(bill);

                //-----------------------------

                List<periodical_action_transanction> _incomeTransactions = new List<periodical_action_transanction>();
                List<periodical_action_transanction> _outcomeTransactions = new List<periodical_action_transanction>();
                //------------------------------------------------------------------------

                //------------ Периодика -------------------------

                Point startPosition = new Point(-1, buttonAddPeriodicalIncome.Location.Y * 2 + buttonAddPeriodicalIncome.Height);
                               
                foreach (Control cnt in tabPagePeriodicalIncome.Controls)
                {
                    if (!(cnt is Button))
                    {
                        tabPagePeriodicalIncome.Controls.Remove(cnt);
                    }
                }
                foreach (Control cnt in tabPagePeriodicalOutcome.Controls)
                {
                    if (!(cnt is Button))
                    {
                        tabPagePeriodicalOutcome.Controls.Remove(cnt);
                    }
                }

                
                List<periodical_action> _outcomePeriodicalActions = new List<periodical_action>();
                List<periodical_action> _incomePeriodicalActions = new List<periodical_action>();                               

                
                foreach (periodical_action act in periodicalActions)
                {
                    if (act.amount > 0)
                    {
                        _incomeTransactions.AddRange(act.periodical_action_transanction);//PeriodicalActionTransactionModel.GetInstance().GetTransactionListByPeriodicalAction(act));
                        _incomePeriodicalActions.Add(act);
                    }
                    else
                    {
                        _outcomeTransactions.AddRange(act.periodical_action_transanction);//PeriodicalActionTransactionModel.GetInstance().GetTransactionListByPeriodicalAction(act));
                        _outcomePeriodicalActions.Add(act);
                    }

                }

                //рендерим периодику
                List<object> _outcomePeriodical = MixedTransactionListSorter.Sort(allocations, _outcomePeriodicalActions);
                List<object> _incomePeriodical = new List<object>(_incomePeriodicalActions.ToArray());
                
                int i = 0;
                foreach (object obj in _outcomePeriodical)
                {
                    PeriodicalActionControl cnt = new PeriodicalActionControl();
                    cnt.Width = tabPagePeriodicalOutcome.Width + 2;
                    cnt.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
                    cnt.Location = new Point(startPosition.X, startPosition.Y + cnt.Height * i - i);
                    i++;
                    cnt.PeriodicalAction = obj;
                    tabPagePeriodicalOutcome.Controls.Add(cnt);
                }
                i = 0;
                foreach (object obj in _incomePeriodical)
                {
                    PeriodicalActionControl cnt = new PeriodicalActionControl();
                    cnt.Width = tabPagePeriodicalIncome.Width + 2;
                    cnt.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
                    cnt.Location = new Point(startPosition.X, startPosition.Y + cnt.Height * i - i);
                    i++;
                    cnt.PeriodicalAction = obj;
                    tabPagePeriodicalIncome.Controls.Add(cnt);
                }

                //-------- Транзакции ---------------------------------------------------
                foreach (Control cnt in tabPageOutcomeStatistic.Controls)
                {
                    if (cnt is TransactionControl)
                    {
                        tabPageOutcomeStatistic.Controls.Remove(cnt);
                    }
                }
                foreach (Control cnt in tabPageIncomeStatistic.Controls)
                {
                    if (cnt is TransactionControl)
                    {
                        tabPageOutcomeStatistic.Controls.Remove(cnt);
                    }
                }



                List<action> _incomeAction = new List<action>();
                List<action> _outcomeAction = new List<action>();

                foreach (action act in actions)
                {
                    if (act.amount > 0)
                    {
                        _incomeAction.Add(act);
                    }
                    else
                    {
                        _outcomeAction.Add(act);
                    }
                }

                List<allocation_transanction> _outcomeAllocationTransactions = new List<allocation_transanction>();
                foreach (allocation alloc in allocations)
                {
                    _outcomeAllocationTransactions.AddRange(alloc.allocation_transanction);//AllocationTransactionModel.GetInstance().GetTransactionListForGoalsByAllocation(alloc));
                }

                List<object> _outcome = MixedTransactionListSorter.Sort(_outcomeAction, _outcomeTransactions, _outcomeAllocationTransactions);
                List<object> _income = MixedTransactionListSorter.Sort(_incomeAction, _incomeTransactions);
                
                Point startPoint = new Point(0, buttonAddIncome.Location.Y * 2 + buttonAddIncome.Height);
                
                i = 0;
                foreach (object obj in _outcome)
                {
                    TransactionControl cntrl = new TransactionControl();
                    cntrl.Transaction = obj;
                    cntrl.Location = new Point(startPoint.X, startPoint.Y + cntrl.Height * i++);
                    tabPageOutcomeStatistic.Controls.Add(cntrl);
                }
                i = 0;
                foreach (object obj in _income)
                {
                    TransactionControl cntrl = new TransactionControl();
                    cntrl.Transaction = obj;
                    cntrl.Location = new Point(startPoint.X, startPoint.Y + cntrl.Height * i++);
                    tabPageIncomeStatistic.Controls.Add(cntrl);
                }
            }
        }
        

        public BillControl()
        {
            InitializeComponent();
        }




        private void buttonTransfer_Click(object sender, EventArgs e)
        {
            TransferMoneyForm frm = new TransferMoneyForm(bill);
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                RefreshControl();
            }
        }

        private void buttonAddIncome_Click(object sender, EventArgs e)
        {
            AddIncomeOutcomeForm frm = new AddIncomeOutcomeForm(ActionType.INCOME, bill);
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                RefreshControl();
            }
        }

        private void buttonAddOutcome_Click(object sender, EventArgs e)
        {
            AddIncomeOutcomeForm frm = new AddIncomeOutcomeForm(ActionType.OUTCOME, bill);
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                RefreshControl();
            }
        }

        private void buttonAddPeriodicalIncome_Click(object sender, EventArgs e)
        {
            AddPeriodicalActionForm frm = new AddPeriodicalActionForm(ActionType.INCOME, this.bill);
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                RefreshControl();
            }
        }

        private void buttonAddPeriodicalOutcome_Click(object sender, EventArgs e)
        {
            AddPeriodicalActionForm frm = new AddPeriodicalActionForm(ActionType.OUTCOME, this.bill);
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                RefreshControl();
            }
        }

        public void RefreshControl()
        {
            Bill = bill;
        }

        private void numericUpDownPersents_ValueChanged(object sender, EventArgs e)
        {
            allocation allocation = AllocationModel.GetInstance().GetAllocationForAccumulationByBilling(bill);
            if ((int)numericUpDownPersents.Value != (int)allocation.amount)
            {
                allocation.amount = (int)numericUpDownPersents.Value;
                AllocationModel.GetInstance().SaveOrUpdateAllocation(allocation);
            }
        }
    }
}
